from pprint import pprint

from xmindparser import xmind_to_dict
import jsonpath
import json

ReadXmind = xmind_to_dict('template.xmind')
# print(ReadXmind)
# print(len(ReadXmind))
# print(type(ReadXmind))
# print(type(ReadXmind[0]))
system = jsonpath.jsonpath(ReadXmind[0],'$.topic.title')[0]
case_content = jsonpath.jsonpath(ReadXmind[0],'$.topic.topics')[0]
cases = jsonpath.jsonpath(case_content[0], '$.topics')[0]
creator = jsonpath.jsonpath(case_content[0], '$.title')[0]
# print('creator--->', creator)
# print("system--->", system)
# print(cases)
# print("模块数--->", len(cases))

for module in cases:
    print("module", module)
    str1 = "{'title': '一级模块1', 'topics': [{'title': '子模块1.1', 'topics': [{'title': '标题1.1.1', 'topics': [{'title': '预置条件1.1.1', 'topics': [{'title': '步骤1.1.1', 'topics': [{'title': '预期1.1', 'topics': [{'title': '用例等级1.1'}]}"\
    # print(module)
    print(str1.count('title'))

    judge_flag_0 = 7
    title_num = json.dumps(module, ensure_ascii=False).count('title')
    if title_num >= judge_flag_0:
        senior_module = jsonpath.jsonpath(module, '$.title')[0]
        # print(senior_module)
        sub_module_list = jsonpath.jsonpath(module, '$.topics')[0]
        print(module)
        for sub_module in sub_module_list:
            # sub_module  一级模块下面的内容
            # print(sub_module)
            # sub_module_name子模块名称
            sub_module_name = jsonpath.jsonpath(sub_module, '$.title')[0]
            # 用例集
            case_list = jsonpath.jsonpath(sub_module, '$.topics')[0]
            for case in case_list:
                # print(case)
                # print(type(case))
                case_to_string = json.dumps(case, ensure_ascii=False)
                # case_to_string.count('title'))) 通过查找title数量，判断文件格式是否符合模板要求
                judge_flag = case_to_string.count('title')
                if judge_flag == 5:
                    case_title = jsonpath.jsonpath(case, '$.title')[0]
                    list1 = jsonpath.jsonpath(case, '$.topics')[0]
                    pre_condition = jsonpath.jsonpath(list1[0], '$.title')[0]
                    list2 = jsonpath.jsonpath(list1[0], '$.topics')[0]
                    case_steps = jsonpath.jsonpath(list2[0], '$.title')[0]
                    list3 = jsonpath.jsonpath(list2[0], '$.topics')[0]
                    case_expect_result = jsonpath.jsonpath(list3[0], '$.title')[0]
                    list4 = jsonpath.jsonpath(list3[0], '$.topics')[0]
                    case_level = jsonpath.jsonpath(list4[0], '$.title')[0]
                    # print("系统名", system)
                    # print("一级模块", senior_module)
                    # print("子模块", sub_module_name)
                    # print("测试标题", case_title)
                    # print("预置条件", pre_condition)
                    # print("步骤", case_steps)
                    # print("预期", case_expect_result)
                    # print("用例等级", case_level)
                    # print("创建人", creator)
        # print(sub_module)
    # print(sub_module)
